package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ContractPriceAndDiscountRateAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.manager.scene.service.QuerySceneInstService;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Objects;

/**
 * 合同单价联动折扣率，折扣率=合同单价/初装费*100%，保留两位小数，置灰
 */
@Service
@AttrHandler(ContractPriceAndDiscountRateAttrHandler.class)
public class ContractPriceAndDiscountRateAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {
    @Autowired
    private QuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        KiteProdInst prodInst = querySceneInstService.getProdInst(data.getCustId(), inst.getInstId());
        List<DcPublic> discountDCs = DcPublicCache.getByPkey("20220222701", prodInst.getProdId());
        if (KiteListUtils.isNotEmpty(discountDCs)) {
            DcPublic dcPublic = discountDCs.get(0);
            KiteAttr discountAttr = inst.getAttr(dcPublic.getPcode());
            KiteAttr upAttrTrue= inst.getAttr(dcPublic.getCodea());
            KiteAttr upAttr = inst.getAttr(dcPublic.getCodeb());
            if (KiteObjectUtils.allNotNull(upAttr, upAttrTrue, discountAttr) && !KiteStringUtils.isAnyEmpty(upAttr.getValue(), upAttrTrue.getValue())) {
                String upValue = upAttr.getValue();
                if(!KiteStringUtils.isNum(upValue)){
                    upValue = upValue.replaceAll("[^0-9]","");
                }
                BigDecimal upNum = new BigDecimal(upValue);
                BigDecimal upTrueNum = new BigDecimal(upAttrTrue.getValue());
                if (KiteStringUtils.isNotEmpty(dcPublic.getCodec())) {
                    upNum = upNum.divide(new BigDecimal(dcPublic.getCodec()));
                }
                if (KiteStringUtils.isNotEmpty(dcPublic.getCoded())) {
                    upTrueNum = upTrueNum.divide(new BigDecimal(dcPublic.getCoded()));
                }
                BigDecimal divide = upTrueNum.multiply(new BigDecimal("100")).divide(upNum, 2, BigDecimal.ROUND_HALF_UP);
                discountAttr.setValue(divide.toString());
                discountAttr.setValueDesc(divide.toString());
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }


}
